package com.jh.paymentchannel.payment.wechat;

import com.jh.paymentchannel.payment.TradeStatus;
import io.jsonwebtoken.lang.Strings;

import java.util.Map;

/**
 * @author Yichuan
 * @version 1.0
 */
public class WechatPayResult {

    private String appId;
    private String bankType;
    private String feeType;
    private String mchId;
    private String nonceStr;
    private String openId;
    private String outTradeNo;
    private String resultCode;
    private String returnCode;
    private String returnMsg;
    private String sign;
    private String totalFee;
    private String tradeType;
    private String tradeNo;
    private String timeEnd;
    private String deviceInfo;
    private TradeStatus status;

    public WechatPayResult() {
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getBankType() {
        return bankType;
    }

    public void setBankType(String bankType) {
        this.bankType = bankType;
    }

    public String getFeeType() {
        return feeType;
    }

    public void setFeeType(String feeType) {
        this.feeType = feeType;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public String getOutTradeNo() {
        return outTradeNo;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.outTradeNo = outTradeNo;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(String returnCode) {
        this.returnCode = returnCode;
    }

    public String getReturnMsg() {
        return returnMsg;
    }

    public void setReturnMsg(String returnMsg) {
        this.returnMsg = returnMsg;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getTotalFee() {
        return totalFee;
    }

    public void setTotalFee(String totalFee) {
        this.totalFee = totalFee;
    }

    public String getTradeType() {
        return tradeType;
    }

    public void setTradeType(String tradeType) {
        this.tradeType = tradeType;
    }

    public String getTradeNo() {
        return tradeNo;
    }

    public void setTradeNo(String tradeNo) {
        this.tradeNo = tradeNo;
    }

    public String getTimeEnd() {
        return timeEnd;
    }

    public void setTimeEnd(String timeEnd) {
        this.timeEnd = timeEnd;
    }

    public String getDeviceInfo() {
        return deviceInfo;
    }

    public void setDeviceInfo(String deviceInfo) {
        this.deviceInfo = deviceInfo;
    }

    public TradeStatus getStatus() {
        return status;
    }

    public void setStatus(TradeStatus status) {
        this.status = status;
    }

    public static WechatPayResult mapToObject(Map<String, String> data) {
        WechatPayResult result = new WechatPayResult();
        result.setReturnCode(data.get("return_code"));
        result.setReturnMsg(data.get("return_msg"));
        result.setAppId(data.get("appid"));
        result.setMchId(data.get("mch_id"));
        result.setDeviceInfo(data.get("device_info"));
        result.setNonceStr(data.get("nonce_str"));
        result.setSign(data.get("sign"));
        result.setResultCode(data.get("result_code"));
        result.setOpenId(data.get("openid"));
        result.setTradeType(data.get("trade_type"));
        result.setBankType(data.get("bank_type"));
        result.setTotalFee(data.get("total_fee"));
        result.setFeeType(data.get("fee_type"));
        result.setOutTradeNo(data.get("out_trade_no"));
        result.setTradeNo(data.get("transaction_id"));
        result.setTimeEnd(data.get("time_end"));
        // 支付回调不返回trade_state参数
        String tradeState = data.get("trade_state");
        if (Strings.hasText(tradeState)) {
            result.setStatus(TradeStatus.wechatState(tradeState));
        } else {
            result.setStatus(TradeStatus.wechatState(result.getResultCode()));
        }
        return result;
    }

    public boolean isSuccess() {
        return getResultCode().equalsIgnoreCase("SUCCESS") &&
                getReturnCode().equalsIgnoreCase("SUCCESS");
    }
}
